package com.example.controller;


import com.example.Dao.UserMapper;
import com.example.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;


import javax.servlet.http.HttpSession;



@Controller
public class LoginController {

    @Autowired
    UserMapper UserMapper;

    @RequestMapping("/user/login")
    public String login(@RequestParam("username") String username, @RequestParam("password") String password, Model model, HttpSession session) {
        User user = UserMapper.selectUser(username);
        if (user != null ) {
            if (password.equals(user.getPassword())) {
                session.setAttribute("loginuser", user.getUsername());
                return "redirect:/main.html";

            } else {
                model.addAttribute("msg", "密码错误,请重新输入");
                return "index";
            }

        }else {
            model.addAttribute("msg", "用户不存在");
            return "index";
        }
    }

        @RequestMapping("/user/loginout")
        public String loginout (HttpSession session){
            session.setAttribute("loginuser", "");
            return "index";
        }

    }
